﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SumiaAccountBook.SubControls
{
    /// <summary>
    /// ucAccountBook.xaml 的交互逻辑
    /// </summary>
    public partial class ucAccountBook : UserControl
    {
        const int PageSize = 20;
        private int page_count = 1;

        public List<Biz.AccountBookObj> AccountBooks { set; get; }

        public ucAccountBook()
        {
            InitializeComponent();
            bindData();
            this.DataContext = this;

        }

        private void bindData()
        {
            int t1 = 0;
            this.AccountBooks = Biz.GetList.GetListAccountBook(page_count, PageSize, ref t1);
            this.dg_accountbook.ItemsSource = this.AccountBooks;
            this.dp_accountbook.TotalCount = t1;
        }

        private void dp_accountbook_PageChanged(object sender, CubicFamily.StockTool.ComonControl.PageChangedEventArgs e)
        {
            this.page_count = e.CurrentPageIndex;
            bindData();
        }

        private void dg_accountbook_remove_Click(object sender, RoutedEventArgs e)
        {
            var original = e.OriginalSource as Button;
            var key = original.CommandParameter;
            int Id = Convert.ToInt32(key);
            MessageBoxResult confirmToDel = MessageBox.Show("确认要删除所选行吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (confirmToDel == MessageBoxResult.Yes)
            {
                try
                {
                    DataAccess.DaCommon<DataAccess.Entity.AccountBook>.Delete(Id);
                }catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
                bindData();
            }
        }

        private void btn_accountbook_add_Click(object sender, RoutedEventArgs e)
        {            
            string value = this.tb_accountbook_name.Text.Trim();
            if (string.IsNullOrEmpty(value))
            {
                MessageBox.Show("请填写名称！");
            }

            try
            {
                DataAccess.DaCommon<DataAccess.Entity.AccountBook>.Add(
                    new DataAccess.Entity.AccountBook { Name = value, Memo = this.tb_accountbook_memo.Text.Trim() });
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
            bindData();
        }
    }
}
